Skip to main content

@pexip/utils

Classes

Interfaces

Type Aliases

AsyncJob

Ƭ AsyncJob: () => Promise<void>

Type declaration

▸ (): Promise<void>

Returns

Promise<void>


Fn

Ƭ Fn<T, R>: (...args: T) => R

Type parameters

NameType
Textends unknown[]
RR

Type declaration

▸ (...args): R

Parameters
NameType
...argsT
Returns

R

Variables

ASTERISK

Const ASTERISK: "\\*"


Constants



BACKSLASH

Const BACKSLASH: "\\\\"


BUILD

Const BUILD: "[0-9a-zA-Z-]+"


CARET

Const CARET: "\\^"


CLOSE_BRACE

Const CLOSE_BRACE: "\\}"


CLOSE_BRACKET

Const CLOSE_BRACKET: "\\]"


CLOSE_PARENTHESE

Const CLOSE_PARENTHESE: "\\)"


DOLLAR

Const DOLLAR: "\\$"


DOT

Const DOT: "\\."


NONNUMERIC

Const NONNUMERIC: "\\d*[a-zA-Z-][a-zA-Z0-9-]*"

Non-numeric Identifier

Zero or more digits, followed by a letter or hyphen, and then zero or more letters, digits, or hyphens.


NUMERIC

Const NUMERIC: "0|[1-9]\\d*"

Numeric Identifier

A single 0, or a non-zero digit followed by zero or more digits.


NUMERICLOSE

Const NUMERICLOSE: "[0-9]+"

Numeric Lose Identifier

At least 1 digit.


OPEN_BRACE

Const OPEN_BRACE: "\\{"


OPEN_BRACKET

Const OPEN_BRACKET: "\\["


OPEN_PARENTHESE

Const OPEN_PARENTHESE: "\\("


PIPE

Const PIPE: "\\|"


PLUS

Const PLUS: "\\+"


QUESTION_MARK

Const QUESTION_MARK: "\\?"


QUEUE_DELAY_IN_MS

Const QUEUE_DELAY_IN_MS: 0


QUEUE_DROP_LAST

Const QUEUE_DROP_LAST: false


QUEUE_SIZE

Const QUEUE_SIZE: 5


QUEUE_THROTTLE_IN_MS

Const QUEUE_THROTTLE_IN_MS: 100

Functions

alt

alt(...patternOrCombinators): Combinator

A Combinator to make the provided patterns as a combination of alternatives

Parameters

NameTypeDescription
...patternOrCombinatorsPatternCombinator[]The combination of alternatives, e.g. a\|b\|c

Returns

Combinator


any

any(patternOrCombinator): Combinator

A Combinator to make the provided pattern's occurrence as any or more, i.e. *

Parameters

NameTypeDescription
patternOrCombinatorPatternCombinatorThe pattern to apply

Returns

Combinator


begin

begin(patternOrCombinator): Combinator

A Combinator for the match start of a string.

Parameters

NameType
patternOrCombinatorPatternCombinator

Returns

Combinator


cancellablePromise

cancellablePromise<A, R>(callback, cleanup?): readonly [(...args: A) => Promise<void | Awaited<R>>, () => void]

Wrapping provided async callback to allow to resolve the async operation with cleanup when the cancel function is called

Type parameters

NameType
Aextends unknown[]
RR

Parameters

NameTypeDescription
callbackAsyncCallback<A, R>An async callback to execute
cleanup?Cleanup<R>A cleanup callback to execute when the provided async callback is canceled

Returns

readonly [(...args: A) => Promise<void | Awaited<R>>, () => void]


createAsyncQueue

createAsyncQueue(«destructured»?): Object

Parameters

NameType
«destructured»AsyncQueueOptions

Returns

Object

NameType
enqueue(job: AsyncJob, runImmediately: boolean) => void
execute() => Promise<void>
get busy()boolean

createQueue

createQueue<T>(maxSize?, initial?, dropLast?): Queue<T>

Type parameters

Name
T

Parameters

NameTypeDefault value
maxSizenumberQUEUE_SIZE
initialT[]undefined
dropLastbooleanQUEUE_DROP_LAST

Returns

Queue<T>


createTokenizer

createTokenizer<T>(): Tokenizer<null | Matched<T>>

Type parameters

NameType
Textends Record<string, unknown>

Returns

Tokenizer<null | Matched<T>>


debounce

debounce<T, R>(fn, waitDuration): (this: unknown, ...args: T) => Promise<R>

An higher-order-function that debounce a given function to only invoke after N amount of time passes since its last call.

Note: Can be extended in the future to allow immediate (leading) flag and cancel method options.

Type parameters

NameType
Textends unknown[]
RR

Parameters

NameTypeDescription
fnFn<T, R>The function to throttle.
waitDurationnumberThe number of milliseconds to wait before invoke.

Returns

fn

▸ (this, ...args): Promise<R>

Parameters
NameType
thisunknown
...argsT
Returns

Promise<R>

NameType
cancel() => void

end

end(patternOrCombinator): Combinator

A Combinator for the match end of a string.

Parameters

NameType
patternOrCombinatorPatternCombinator

Returns

Combinator


endsWithAnyOf

endsWithAnyOf(subject, possibleMatches): boolean

Parameters

NameType
subjectstring
possibleMatchesstring[]

Returns

boolean


firstOr

firstOr<T>(list, defaultValue): T

Selects the first element in a list

Type parameters

Name
T

Parameters

NameType
listT[]
defaultValueT

Returns

T

T - if list[index] === undefined, it returns defaultValue


getBaseURI

getBaseURI(node?): string

Parameters

NameTypeDefault value
nodeObjectdocument
node.baseURIstringundefined

Returns

string


getTimeLeft

getTimeLeft(futureTimestampSeconds, earlierTimestampSeconds): string

Parameters

NameType
futureTimestampSecondsnumber
earlierTimestampSecondsnumber

Returns

string


group

group(patternOrCombinator, name?, valueType?): Combinator

A Combinator to group pattern. When name is provided, it will be one of the keys that returned from

See

  • Parser. This will be the main function to extract information for parsing
  • ValueType

Default Value

'string'

Parameters

NameTypeDescription
patternOrCombinatorPatternCombinatorThe pattern to group
name?stringThe name for the capturing. when no provided, there is no capturing
valueType?ValueTypeThe type that the capture should be.

Returns

Combinator


hasOwn

hasOwn<T, K>(obj, prop): obj is T & Record<K, unknown>

A type utility function to check if the provided object has the prop, and provide a proper type to workaround Typescript's limitation

Type parameters

NameType
Textends object
Kextends PropertyKey

Parameters

NameTypeDescription
objTThe object to check
propKThe prop to be used for the checking

Returns

obj is T & Record<K, unknown>


identity

identity<T>(x): T

Type parameters

Name
T

Parameters

NameType
xT

Returns

T


isDefined

isDefined<T>(t?): Object

Test that a value t is set to something and return it, throws on undefined.

This is handy when selecting using an index to eliminiate the infered undefined type.

Example

console.log(isDefined('')); // =\> ''
console.log(isDefined(0)); // =\> 0
console.log(isDefined(false)); // =\> false
console.log(isDefined(null)); // =\> null
console.log(isDefined(undefined)); // will throw

Type parameters

Name
T

Parameters

NameType
t?T

Returns

Object


isEmpty

isEmpty<T, K>(obj): obj is undefined | EmptyArray<T> | Record<K, never>

Verify if the provided unknown object is empty

Check the test cases for the assumptions

Type parameters

NameType
TT
Kextends string | number | symbol

Parameters

NameTypeDescription
objundefined | Partial<Record<K, unknown>> | T[]An object to be tested

Returns

obj is undefined | EmptyArray<T> | Record<K, never>


join

join(...patternOrCombinators): Combinator

A Combinator to join the provided patterns as a single pattern

Parameters

NameTypeDescription
...patternOrCombinatorsPatternCombinator[]The patterns to join.

Returns

Combinator


lastOr

lastOr<T>(list, defaultValue): T

Selects the last element in a list

Type parameters

Name
T

Parameters

NameType
listT[]
defaultValueT

Returns

T

T - if list[index] === undefined, it returns defaultValue


noop

noop(): undefined

A no-operation function.

Example

const object = { 'name': 'fred' };
noop(object) === undefined;
// => true

Returns

undefined


nthOr

nthOr<T>(list, index, defaultValue): T

Inspired by lodash nth this will select the given index from an array. As with the lodash version of nth it will select from the end of the array given a negative index.

Type parameters

Name
T

Parameters

NameType
listT[]
indexnumber
defaultValueT

Returns

T

T - if list[index] === undefined, it returns defaultValue


nthThrows

nthThrows<T>(list, index): Object

Inspired by lodash nth this will select the given index from an array, but throw if the value is undefined. As with the lodash version of nth it will select from the end of the array given a negative index. This is likely to be most useful in tests. You might want nthOr, firstOr, or lastOr instead

Type parameters

Name
T

Parameters

NameType
listT[]
indexnumber

Returns

Object


option

option(patternOrCombinator): Combinator

A Combinator to make the provided pattern's occurrence as optional. i.e. ?

Parameters

NameTypeDescription
patternOrCombinatorPatternCombinatorThe pattern to apply

Returns

Combinator


pipe

pipe<T, R>(fn1, ...fns): (...args: T) => R

Pipes the value of an expression into a pipeline of functions.

Example

const len = (s: string): number => s.length;
const double = (n: number): number => n * 2;

// Without pipe

const doubleLen = (s: string) => double(len(s));

// With pipe
const doubleLen = pipe(len, double);

Type parameters

NameType
Textends unknown[]
RR

Parameters

NameType
fn1Fn<T, R>
...fnsRns<R>

Returns

fn

▸ (...args): R

Parameters
NameType
...argsT
Returns

R


prig

prig(baseIndex, shuffleID, listSize): number

Pseudorandom index generator implemented with Miller Shuffle Algorithm D variant NEW April 2023 aka: MillerShuffleAlgo_d

It produces a shuffled Index given a base Index, a shuffle ID "seed" and the length of the list being indexed. For each inx: 0 to listSize-1, unique indexes are returned in a pseudo "random" order. Utilizes minimum resources. As such there is no better choice for a playlist shuffle.

See

Parameters

NameTypeDescription
baseIndexnumberThe index to be shuffled
shuffleIDnumberThe seed number
listSizenumberThe size of the list

Returns

number

shuffled index


range

range(patternOrCombinator, min, max?): Combinator

A Combinator to make the provided pattern's occurrence as in specified range. i.e. {min,max}

Parameters

NameTypeDescription
patternOrCombinatorPatternCombinatorThe pattern to apply
minnumberThe minimum occurrence
max?numberThe Maximum occurrence, undefined means unlimited

Returns

Combinator


subscribeEvent

subscribeEvent<T>(target, type, handler, options?): UnsubscribeFn

Subscribe an EventTarget's event and return a unsubscribe function

Type parameters

NameType
Textends EventTarget

Parameters

NameTypeDescription
targetTAn EventTarget to be subscribed
typeParams<T["addEventListener"]>[0]The type to pass into addEventListener function
handlerParams<T["addEventListener"]>[1]The handle to pass into addEventListener function
options?Params<T["addEventListener"]>[2]The options to pass into addEventListener function

Returns

UnsubscribeFn


throttle

throttle<T, R>(fn, wait?): (...args: T) => void

Creates a throttled function that only invokes func at most once per every wait milliseconds

Type parameters

NameType
Textends unknown[]
RR

Parameters

NameTypeDefault valueDescription
fnFn<T, R>undefinedThe function to throttle.
waitnumber0The number of milliseconds to throttle invocations to.

Returns

fn

▸ (...args): void

Parameters
NameType
...argsT
Returns

void